<html>
<head><meta charset="utf-8"><title>NonNull covariance questions · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/NonNull.20covariance.20questions.html">NonNull covariance questions</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="167558831"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/NonNull%20covariance%20questions/near/167558831" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/NonNull.20covariance.20questions.html#167558831">(Jun 07 2019 at 08:44)</a>:</h4>
<p>I'm trying to wrap a <code>*mut SDL_Palette</code> to make it easier to work with. Now, this is aliased data that never goes across threads. I think that NonNull might fight my use case well. What worries me is the "covariance" thing. The rustonomicon explains Covariance in terms of subtyping, but since this inner pointer itself will never be overwritten by any other value of any other type, I'm not sure if it affects my case. All the data you edit lives on the far side of the pointer and it's not send, so there's methods like this</p>
<div class="codehilite"><pre><span></span><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">set_colors</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">start</span>: <span class="kt">usize</span><span class="p">,</span><span class="w"> </span><span class="n">new_colors</span>: <span class="kp">&amp;</span><span class="p">[</span><span class="n">Color</span><span class="p">])</span><span class="w"> </span>-&gt; <span class="nb">Result</span><span class="o">&lt;</span><span class="p">(),</span><span class="w"> </span><span class="nb">String</span><span class="o">&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="p">...</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
</pre></div>


<p>which should be sound (but also tell me if it's not)</p>



<a name="167559047"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/NonNull%20covariance%20questions/near/167559047" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/NonNull.20covariance.20questions.html#167559047">(Jun 07 2019 at 08:47)</a>:</h4>
<p>Complete file: <a href="https://github.com/Lokathor/beryllium/blob/22f836228b93da8450c5861b9e9004d3171be6ac/src/palette.rs" target="_blank" title="https://github.com/Lokathor/beryllium/blob/22f836228b93da8450c5861b9e9004d3171be6ac/src/palette.rs">https://github.com/Lokathor/beryllium/blob/22f836228b93da8450c5861b9e9004d3171be6ac/src/palette.rs</a></p>



<a name="171381863"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/NonNull%20covariance%20questions/near/171381863" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/NonNull.20covariance.20questions.html#171381863">(Jul 21 2019 at 18:02)</a>:</h4>
<ul>
<li>
<p>covariance does not matter since <code>SDL_Palette</code> does not have generic type/lifetime parameters;</p>
</li>
<li>
<p><code>set_colors</code> performing mutation is fine as long as <code>Self : !Sync</code></p>
</li>
</ul>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>